<?php

namespace App\Http\Controllers\Admin;
use App\Models\Shop;
use App\Models\WaterBillCategory;


/**
 * 商品分类管理控制器
 */
class WaterBillCategoryController extends BaseController
{

    public function __construct(WaterBillCategory $waterBillCategory,Shop $shop)
    {
        parent::__construct();
        $this->waterbillCategory = $waterBillCategory;
        $this->shop = $shop;
    }

    /**
     * 分类管理列表
     */
    public function getIndex()
    {
        $shopList = $this->shop->_getList(['admin_id'=>is_login()])->toArray();
        $tree = $this->waterbillCategory->_getList(['admin_id'=>is_login()],'*',['sort'=>'asc'])->toArray();
        return view('admin.waterbill_category.index', compact('tree','shopList'));
    }


    /* 新增分类 */
    public function getAdd()
    {
        return view('admin.waterbill_category.edit');
    }

    public function postAdd()
    {
        $res = $this->waterbillCategory->addOrUpdate();
        if (!$res) {
            return $this->error($this->waterbillCategory->getError() ? $this->waterbillCategory->getError() : '新增失败');
        }
        return $this->success('新增成功', urlAdmin('waterBillCategory', 'getIndex'));
    }

    /* 编辑分类 */
    public function getEdit()
    {
        $info = $this->waterbillCategory->_getOneById(request('id'));
        return view('admin.waterbill_category.edit', compact('info'));
    }

    public function postEdit()
    {
        $res = $this->waterbillCategory->addOrUpdate();
        if (!$res) {
            return $this->error($this->waterbillCategory->getError() ? $this->waterbillCategory->getError() : '未知错误');
        }
        return $this->success('更新成功', urlAdmin('waterBillCategory', 'getIndex', ['pid' => request('pid')]));
    }

}
